package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.mould.MouldBomDetailDTO;
import com.jcfk.eam.domain.dto.mould.MouldBomGroupDTO;
import com.jcfk.eam.domain.query.mould.MouldBomGroupQuery;
import com.jcfk.eam.service.mould.MouldBomGroupService;
import com.jcfk.eam.util.bean.codeParse.MouldCodeParse;
import com.jcfk.eam.util.enums.CodeParseEnu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 模具bom组合表
 *
 * @author yrl
 * @email
 * @date 2024-11-08
 */
@Api(tags = {"模具bom组合表管理"})
@RestController
@RequestMapping("modbomgroup")
public class MouldBomGroupController {
    @Autowired
    private MouldBomGroupService modBomGroupService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<MouldBomGroupQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具bom组合表")
    @PostMapping("/queryByPage")
    public RetPage<MouldBomGroupDTO> queryByPage(@RequestBody ReqPageParam<MouldBomGroupQuery> requestParam) {
        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<MouldBomGroupQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<MouldBomGroupDTO> page = new DataPage<MouldBomGroupDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                MouldBomGroupDTO.class);
        page = modBomGroupService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MouldBomGroupQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具bom组合表列表", notes = "获取模具bom组合表列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<MouldBomDetailDTO>> getDetails(@RequestBody MouldBomGroupQuery query) {

        List<MouldBomDetailDTO> list = modBomGroupService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MouldBomGroupQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具bom组合表列表", notes = "获取模具bom组合表列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<MouldBomGroupDTO>> getList(@RequestBody MouldBomGroupQuery query) {
        if (query.getDeleted() == null)
            query.setDeleted("0");
        if (query.getEnabled() == null)
            query.setEnabled("1");

        List<MouldBomGroupDTO> list = modBomGroupService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 模具bom组合表
     * 根据id 查询详情
     */
    @ApiOperation("查询模具bom组合表")
    @GetMapping("/get/{id}")
    public RetResult<MouldBomGroupDTO> get(@PathVariable("id") String id) {
        MouldBomGroupDTO item = modBomGroupService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模具bom组合表
     */
    @ApiOperation("保存模具bom组合表")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具bom组合表管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid MouldBomGroupDTO modBomGroup) {
        // 数据校验
        RetResult ret = modBomGroupService.isValid(modBomGroup);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(modBomGroup.getId())) {
            // 新增
            modBomGroupService.insert(modBomGroup);
        } else {
            // 更新
            modBomGroupService.update(modBomGroup);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除模具bom组合表
     */
    @ApiOperation("批量删除模具bom组合表")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具bom组合表管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody MouldBomGroupDTO modBomGroup) {
        return RetResult.ok(modBomGroupService.logicDeleteList(modBomGroup));
    }


    /**
     * 删除模具bom组合表
     */
    @ApiOperation("删除模具bom组合表")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具bom组合表管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        modBomGroupService.deleteItem(id);
        return RetResult.ok();
    }

    /**
     * 批量停用模具bom
     */
    @ApiOperation("停用模具bom")
    @PostMapping("/disabledList")
    public RetResult<?> disabledList(@RequestBody MouldBomGroupDTO MouldBomGroupDTO) {
        modBomGroupService.disabledList(MouldBomGroupDTO);
        return RetResult.ok();
    }

//--------------------------------------------------------------PDA------------------------------------------------------------------------------

    /**
     * 通过模具编码查询bom组合
     *
     * @param mouldCode 模具编码
     */
    @ApiOperation("查询模具bom组合表")
    @GetMapping("/getListByMouldCode/{mouldCode}")
    public RetResult<?> getListByMouldCode(@PathVariable("mouldCode") String mouldCode) {
        CodeParseEnu.MOULD_CODE.matcherThrow(mouldCode);
        MouldCodeParse parse = (MouldCodeParse) CodeParseEnu.MOULD_CODE.parse(mouldCode);
        MouldBomGroupQuery query = new MouldBomGroupQuery();
        query.setDeleted("0");
        query.setEnabled("1");
        query.setMaterialCode(parse.getMaterialCode());
        List<MouldBomGroupDTO> bomGroupList = modBomGroupService.getList(query);
        return RetResult.ok(bomGroupList);
    }

}
